//
//  SEExamineViewController.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/7.
//

import UIKit

class SEExamineViewController: SEBaseViewController {


    @IBOutlet weak var tableView: UITableView! {
        didSet {
            tableView.tableFooterView = UIView()
            tableView.register(UINib(nibName: SEExamineEntranceCell.className,
                                     bundle: nil),
                               forCellReuseIdentifier: SEExamineEntranceCell.reuseId)
            tableView.register(UINib(nibName: SEExaminePosterCell.className,
                                     bundle: nil),
                               forCellReuseIdentifier: SEExaminePosterCell.reuseId)
        }
    }

    var viewModel = SEExamineViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        title = "社区检查"
        // Do any additional setup after loading the view.
    }

    override func setupUI() {
        super.setupUI()
        viewModel.updateData = { status in
            switch status {
            case .success:
                self.handlerHospital()
            case .auth(let msg):
                self.presentLoginViewController(selector: #selector(self.requestHospital)) {
                    SEHUD.showError(message: msg)
                }
            case .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }
    }

    func pushViewController(viewType: SEExamineEntranceBtnViewType) {
        switch viewType {
        case .reserve:
            pushHospitalListVc()
        case .scan:
            pushScanVc()
        case .record:
            pushExamineListVc()
        }
    }

    private func handlerHospital() {
        if viewModel.hospitalViewModel.models.count == 1 {
            let vc = SEExamineReserveViewController(with: viewModel.hospitalViewModel.models[0], checkSource: 2)
            navigationController?.pushViewController(vc, animated: true)
        } else {
            SEHUD.showError(message: "未查询到医院，请手动选择")
            pushHospitalListVc()
        }
    }

    private func pushHospitalListVc() {
        let vc = SEHospitalListViewController(with: .examine)
        navigationController?.pushViewController(vc, animated: true)
    }

    private func pushScanVc() {
        let scanVC = QRCodeScanVC()
        scanVC.handlerResult = { (jsonStr, scanFrom) in
            var deviceId: String?
            if let urlStr = jsonStr {
                let deviceIdStr = urlStr.components(separatedBy: "?").last
                deviceId = deviceIdStr?.components(separatedBy: "=").last
            }
            self.viewModel.hospitalViewModel.deviceId = deviceId ?? ""
            self.requestHospital()
        }
        self.navigationController?.pushViewController(scanVC, animated: true)
    }

    private func pushExamineListVc() {
        let vc = SEExamineListViewController()
        navigationController?.pushViewController(vc, animated: true)
    }

}

// MARK: - UITableViewDataSource, UITableViewDelegate

extension SEExamineViewController: UITableViewDataSource, UITableViewDelegate {

    // MARK: - UITableViewDataSource

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: SEExamineEntranceCell.reuseId, for: indexPath) as! SEExamineEntranceCell
            cell.examineEntranceCellTapClosure = {[weak self] viewType in
                self?.pushViewController(viewType: viewType)
            }
            return cell
        }
        if indexPath.section == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: SEExaminePosterCell.reuseId, for: indexPath) as! SEExaminePosterCell
            return cell
        }
        return UITableViewCell()
    }

    // MARK: - UITableViewDelegate

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        switch indexPath.section {
        case 0: return 140
        case 1: return UIScreen.screenWidth / 375.0 * 643.0
        default: return 0
        }
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = UIColor.clear
        return view
    }
}

// MARK: - APIRequest
extension SEExamineViewController {
    @objc func requestHospital() {
        viewModel.requestHospital()
    }
}
